package com.ruoyi.generator.component;

import com.ruoyi.generator.entity.MaintenancePlan;
import com.ruoyi.generator.entity.MaintenancePlanHistory;
import com.ruoyi.generator.entity.PlanSchedule;
import com.ruoyi.generator.entity.PlanScheduleHistory;
import com.ruoyi.generator.entity.bo.PlanScheduleInfoBO;
import com.ruoyi.generator.mapper.AppMapper;
import com.ruoyi.generator.mapper.MaintenanceMapper;
import com.ruoyi.generator.mapper.PlanScheduleHistoryMapper;
import com.ruoyi.generator.mapper.PlanScheduleMapper;
import com.ruoyi.generator.vo.MaintenanceAppVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@EnableScheduling
public class ScheduleUpdate {
    @Autowired
    private PlanScheduleMapper planScheduleMapper;
    @Autowired
    private MaintenanceMapper maintenanceMapper;
    @Autowired
    private AppMapper appMapper;


    @Autowired
    private PlanScheduleHistoryMapper planScheduleHistoryMapper;

    /**
     * TZ
     */
    @Scheduled(cron = "0 */5 * * * ?")
    public void updatetime() {
        PlanSchedule planSchedule = new PlanSchedule();
        List<PlanScheduleInfoBO> planScheduleInfoBOList = planScheduleMapper.selectPlanScheduleListwait(planSchedule);
        //获取当前时间
        LocalDateTime now = LocalDateTime.now();
        // 定义时间格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm");
        // 格式化当前时间
        String formattedTime = now.format(formatter);
        // 字符串转LocalDateTime
        LocalTime timem = LocalTime.parse(formattedTime, formatter);
        // 提取分钟值
        Integer minutes = timem.getHour() * 60 + timem.getMinute();
        PlanSchedule planSchedule1 = new PlanSchedule();
        //未开始列表修改为运行状态
        for (PlanScheduleInfoBO planScheduleInfoBO : planScheduleInfoBOList) {
            if (planScheduleInfoBO.getTime().contains("当日")) {
                //截取时间
                String  endTime= planScheduleInfoBO.getTime().substring(planScheduleInfoBO.getTime().length() - 5);
                String  time= planScheduleInfoBO.getTime().substring(2, 7);
                // 字符串转LocalDateTime
                LocalTime timem1 = LocalTime.parse(time, formatter);
                LocalTime timem2 = LocalTime.parse(endTime, formatter);
                Integer minutes2 = timem2.getHour() * 60 + timem2.getMinute();
                Integer minutes1 = timem1.getHour() * 60 + timem1.getMinute();
                if(timem2.getHour()==0){
                    minutes2=24*60+timem2.getMinute();
                }
                // 提取分钟值

                //开始时间对比当前时间
                if (minutes > minutes1 && minutes < minutes2) {
                    //修改状态为运行中
                    planSchedule1.setPlanScheduleId(planScheduleInfoBO.getPlanScheduleId());
                    planSchedule1.setCheckStatus("运行中");
                    planScheduleMapper.updatePlanSchedule(planSchedule1);
                }
                if (minutes > minutes2) {
                    //新增点检历史
                    PlanScheduleHistory planScheduleHistory = new PlanScheduleHistory();
                    planScheduleHistory.setPlanScheduleId(Long.valueOf(planScheduleInfoBO.getPlanScheduleId()));
                    planScheduleHistory.setPlanAssociationId(Long.valueOf(planScheduleInfoBO.getPlanAssociationId()));
                    planScheduleHistory.setScheduleId(Long.valueOf(planScheduleInfoBO.getScheduleId()));
                    planScheduleHistory.setPeriod(planScheduleInfoBO.getPeriod());
                    planScheduleHistory.setShiftDefaultId(Long.valueOf(planScheduleInfoBO.getShiftDefaultId()));
                    planScheduleHistory.setCreateTime(LocalDate.now());
                    planScheduleHistory.setCreatepsTime(LocalDate.parse(planScheduleInfoBO.getCreatepsTime().substring(0,10)));

                    //修改状态为运行中
                    planSchedule1.setPlanScheduleId(planScheduleInfoBO.getPlanScheduleId());
                    planSchedule1.setCheckStatus("已过期");
                    planScheduleMapper.updatePlanSchedule(planSchedule1);
                    //增加过期数据为历史数据
                    planScheduleHistory.setCheckStatus("已过期");
                    planScheduleHistoryMapper.insertPlanScheduleHistory(planScheduleHistory);
                }
            }
        }
    }

//    @Scheduled(cron = "0 0 0 * * ?")
//    public void updatedayweek() {
//        PlanSchedule planSchedule = new PlanSchedule();
//        List<PlanScheduleInfoBO> planScheduleInfoBOList = planScheduleMapper.selectPlanScheduleListwait(planSchedule);
//        // 获取当前日期
//        LocalDate today = LocalDate.now();
//        // 获取当月的第几天（数字，如9）
//        int dayOfMonth = today.getDayOfMonth();
//        // 获取周几的数字（1=周一，7=周日）
//        int dayOfWeekValue = today.getDayOfWeek().getValue();
//        PlanSchedule planSchedule1 = new PlanSchedule();
//        //未开始列表修改为运行状态
//        for (PlanScheduleInfoBO planScheduleInfoBO : planScheduleInfoBOList) {
//            //新增点检历史
//            PlanScheduleHistory planScheduleHistory = new PlanScheduleHistory();
//            planScheduleHistory.setPlanScheduleId(Long.valueOf(planScheduleInfoBO.getPlanScheduleId()));
//            planScheduleHistory.setPlanAssociationId(Long.valueOf(planScheduleInfoBO.getPlanAssociationId()));
//            planScheduleHistory.setScheduleId(Long.valueOf(planScheduleInfoBO.getScheduleId()));
//            planScheduleHistory.setPeriod(planScheduleInfoBO.getPeriod());
//            planScheduleHistory.setShiftDefaultId(Long.valueOf(planScheduleInfoBO.getShiftDefaultId()));
//            planScheduleHistory.setCreateTime(LocalDate.now());
//            planScheduleHistory.setCreatepsTime(LocalDate.parse(planScheduleInfoBO.getCreatepsTime().substring(0,10)));
//            if (planScheduleInfoBO.getTime().contains("日") && planScheduleInfoBO.getTime().length() < 6) {
//                String input = planScheduleInfoBO.getTime();
//                String day = input.replaceAll("[^0-9]", ""); // 提取数字部分
//                if (Integer.parseInt(day) == dayOfMonth) {
//                    //修改状态为运行中
//                    planSchedule1.setPlanScheduleId(planScheduleInfoBO.getPlanScheduleId());
//                    planSchedule1.setCheckStatus("运行中");
//                    planScheduleMapper.updatePlanSchedule(planSchedule1);
//                } else if (Integer.parseInt(day) < dayOfMonth) {
//                    //修改状态为已过期
//                    planSchedule1.setPlanScheduleId(planScheduleInfoBO.getPlanScheduleId());
//                    planSchedule1.setCheckStatus("已过期");
//                    planScheduleMapper.updatePlanSchedule(planSchedule1);
//
//                    //修改历史状态
//                    planScheduleHistory.setCheckStatus("已过期");
//                    planScheduleHistoryMapper.insertPlanScheduleHistory(planScheduleHistory);
//                }
//            }
//            if (planScheduleInfoBO.getTime().contains("周")) {
//                if (dayOfWeekValue == getDayNumber(planScheduleInfoBO.getTime())) {
//                    //修改状态为运行中
//                    planSchedule1.setPlanScheduleId(planScheduleInfoBO.getPlanScheduleId());
//                    planSchedule1.setCheckStatus("运行中");
//                    planScheduleMapper.updatePlanSchedule(planSchedule1);
//                } else if (dayOfWeekValue > getDayNumber(planScheduleInfoBO.getTime())) {
//                    //修改状态为已过期
//                    planSchedule1.setPlanScheduleId(planScheduleInfoBO.getPlanScheduleId());
//                    planSchedule1.setCheckStatus("已过期");
//                    planScheduleMapper.updatePlanSchedule(planSchedule1);
//
//                    //修改历史状态
//                    planScheduleHistory.setCheckStatus("已过期");
//                    planScheduleHistoryMapper.insertPlanScheduleHistory(planScheduleHistory);
//                }
//            }
//        }
//    }
//
//
    @Scheduled(cron = "0 0 0 * * ?")//每天重置任务
//    @Scheduled(cron = "0 */1 * * * ?")
    public void updatePlanSchedulHistory() {
        PlanSchedule planSchedule = new PlanSchedule();
        List<PlanScheduleInfoBO> planScheduleInfoBOList = planScheduleMapper.selectPlanScheduleListAll(planSchedule);
        for (PlanScheduleInfoBO planScheduleInfoBO : planScheduleInfoBOList) {
            if (planScheduleInfoBO.getTime().contains("当日")) {}
                planScheduleMapper.updateAllPs(planScheduleInfoBO.getPlanScheduleId().toString());
        }
    }
//    //执行每周任务重置 每月末重置任务
//    @Scheduled(cron = "0 0 0 ? * 1")
//    public void updatePlantenanceweek() {
//
//        PlanSchedule planSchedule = new PlanSchedule();
//        List<PlanScheduleInfoBO> planScheduleInfoBOList = planScheduleMapper.selectPlanScheduleListAll(planSchedule);
//        for (PlanScheduleInfoBO planScheduleInfoBO : planScheduleInfoBOList) {
//            if (planScheduleInfoBO.getTime().contains("日") && planScheduleInfoBO.getTime().length() < 6) {
//                planScheduleMapper.updateAllPs(planScheduleInfoBO.getPlanScheduleId().toString());
//            }
//        }
//    }
//    //执行每周任务重置 每周重置任务
//    @Scheduled(cron = "0 0 0 L * ?")
//    public void updatePlantenancemonth() {
//        PlanSchedule planSchedule = new PlanSchedule();
//        List<PlanScheduleInfoBO> planScheduleInfoBOList = planScheduleMapper.selectPlanScheduleListwait(planSchedule);
//        for (PlanScheduleInfoBO planScheduleInfoBO : planScheduleInfoBOList) {
//            if (planScheduleInfoBO.getTime().contains("周")) {}
//            planScheduleMapper.updateAllPs(planScheduleInfoBO.getPlanScheduleId().toString());
//        }
//    }





    //保养每日执行的定时
    @Scheduled(cron = "0 */5 * * * *")
    public void updateMaintenanceStatusday() {
        MaintenanceAppVO m1=new MaintenanceAppVO();
        m1.setName("每日");
        List<MaintenanceAppVO> maintenanceList = appMapper.getMaintenanceListByName(m1);
        LocalDate today = LocalDate.now();
        for (MaintenanceAppVO item : maintenanceList) {
                    handleDailyOperation(item);
                    String[] times = item.getTime().split("-");
                    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm");
                    //截取时间
                    String time = times[0];
                    String endTime = times[1];
                    // 字符串转LocalDateTime
                    LocalTime timem1 = LocalTime.parse(time, formatter);
                    LocalTime timem2 = LocalTime.parse(endTime, formatter);
                    Integer minutes2 = timem2.getHour() * 60 + timem2.getMinute();
                    Integer minutes1 = timem1.getHour() * 60 + timem1.getMinute();
                    if (timem2.getHour() == 0) {
                        minutes2 = 24 * 60 + timem2.getMinute();
                    }
                    //获取当前时间
                    LocalDateTime now = LocalDateTime.now();
                    // 定义时间格式
                    // 格式化当前时间
                    String formattedTime = now.format(formatter);
                    // 字符串转LocalDateTime
                    LocalTime timem = LocalTime.parse(formattedTime, formatter);
                    // 提取分钟值
                    Integer minutes = timem.getHour() * 60 + timem.getMinute();
                    if (minutes2 < minutes) {
                        //修改为已过期
                        item.setMaintenanceStatus("2");
                        insertMhistory(item);
                        appMapper.updateMaintenanceProblemStatus(item);
                    }
                    if(minutes2>minutes && minutes>minutes1){
                        //修改运行中
                        item.setMaintenanceStatus("4");
                        appMapper.updateMaintenanceProblemStatus(item);
                    }
        }
    }
    //保养每天执行的定时
    @Scheduled(cron = "0 0 0 * * ?")
    public void updateMaintenanceStatuswm() {
        List<MaintenanceAppVO> maintenanceList = appMapper.getMaintenanceList("");
        LocalDate today = LocalDate.now();
        for (MaintenanceAppVO item : maintenanceList) {
            if(item.getName().contains("每周")) {
                handleWeeklyOperation(item, today);
                // 提取中文数字
                String[] parts = item.getTime().split("到");
                String startDay = parts[0].replace("周", "");
                String endDay = parts[1].replace("周", "");
                // 转换为数字
                int startNum = convertChineseNumber(startDay);
                int endNum = convertChineseNumber(endDay);
                DayOfWeek day = today.getDayOfWeek();
                int dayweek = day.getValue();    // 1-7对应周一到周日
                if (dayweek > endNum) {
                    //修改为已过期
                    item.setMaintenanceStatus("2");
                    insertMhistory(item);
                    appMapper.updateMaintenanceProblemStatus(item);
                }
                if(dayweek<endNum && dayweek >startNum){
                    //修改为运行中
                    item.setMaintenanceStatus("4");
                    appMapper.updateMaintenanceProblemStatus(item);
                }
            }
            if(item.getName().contains("每月")) {
                String[] partsr = item.getTime().split("日-|日");
                String startDaya = partsr[0].substring(2); // 从第3字符开始截取
                String endDaya = partsr[1];
                int end = Integer.parseInt(endDaya);
                int start = Integer.parseInt(startDaya);
                int daynow = today.getDayOfMonth();
                if (daynow > end) {
                    //修改为已过期
                    item.setMaintenanceStatus("2");
                    insertMhistory(item);
                    appMapper.updateMaintenanceProblemStatus(item);
                }
                if(daynow<end && daynow >start){
                    //修改为运行中
                    item.setMaintenanceStatus("4");
                    appMapper.updateMaintenanceProblemStatus(item);
                }
            }
            if(item.getName().contains("季度")) {
                if(item.getMaintenanceStatus().equals("0")){
                    //修改为运行中
                    item.setMaintenanceStatus("4");
                    appMapper.updateMaintenanceProblemStatus(item);
                }
            }
            if(item.getName().contains("年度")) {
                String[] dates = item.getTime().replace("每年", "")
                        .split("-");
                // 解析为LocalDate对象（自动补全年份）
                // 定义匹配"MM月dd日"的格式器

                DateTimeFormatter formatter = new DateTimeFormatterBuilder()
                        .appendPattern("MM月dd日")
                        .parseDefaulting(ChronoField.YEAR, today.getYear())  // 设置默认年份
                        .toFormatter();
                LocalDate date = LocalDate.parse(dates[0], formatter);
                LocalDate endDate = LocalDate.parse(dates[1], formatter);
                DateTimeFormatter formattera = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                String formattedDate = today.format(formatter);
                LocalDate parsedDate = LocalDate.parse(formattedDate, formatter);
                if (parsedDate.isAfter(endDate)) {
                    //修改为已过期
                    item.setMaintenanceStatus("2");
                    insertMhistory(item);
                    appMapper.updateMaintenanceProblemStatus(item);
                }
                if(parsedDate.isBefore(endDate)&&parsedDate.isAfter(date)){
                    //修改为已过期
                    item.setMaintenanceStatus("4");
                    appMapper.updateMaintenanceProblemStatus(item);
                }
            }

        }
    }

    public void insertMhistory(MaintenanceAppVO m1){
        MaintenancePlanHistory m=new MaintenancePlanHistory();
        m.setMaintenanceId(m1.getMaintenanceId());
        m.setEquipmentId(m1.getEquipmentId());
        m.setIsDelete(m1.getIsDelete());
        m.setMaintenancePoint(m1.getMaintenancePoint());
        m.setMaintenanceItem(m1.getMaintenanceItem());
        m.setSetValue(m1.getSetValue());
        m.setMaintenancePeriod(m1.getMaintenancePeriod());
        m.setStartDate(m1.getStartDate());
        m.setMaintenanceStatus(m1.getMaintenanceStatus());
        m.setTime(m1.getTime());
        m.setName(m1.getName());
        m.setRemark(m1.getRemark());
        m.setProblemStatus(m1.getProblemStatus());
        m.setType(m1.getType());
        maintenanceMapper.insertWushuiMaintenancePlanHistory(m);
    }


    //执行每天任务重置 每天十二点重置任务
    @Scheduled(cron = "0 0 0 * * ?")
//    @Scheduled(cron = "0 */2 * * * *")
    public void updateMaintenanceday() {
        MaintenancePlan m=new MaintenancePlan();
        m.setMaintenancePeriod("1");
        maintenanceMapper.updatePlanMstatus(m);
    }
    //执行每周任务重置 每周末重置任务
    @Scheduled(cron = "0 0 0 ? * 1")
//    @Scheduled(cron = "0 */2 * * * *")
    public void updateMaintenanceweek() {
        MaintenancePlan m=new MaintenancePlan();
        m.setMaintenancePeriod("2");
        maintenanceMapper.updatePlanMstatus(m);
    }
    //执行每月任务重置 每月月底重置任务
    @Scheduled(cron = "0 0 0 L * ?")
//    @Scheduled(cron = "0 */2 * * * *")
    public void updateMaintenancemonth() {
        MaintenancePlan m=new MaintenancePlan();
        m.setMaintenancePeriod("3");
        maintenanceMapper.updatePlanMstatus(m);
    }
    //执行每年任务重置 每年12.31重新创建任务
    @Scheduled(cron = "0 0 0 31 12 ?")
//        @Scheduled(cron = "0 */2 * * * *")
    public void updateMaintenanceyear() {
        MaintenancePlan m=new MaintenancePlan();
        m.setMaintenancePeriod("5");
        maintenanceMapper.updatePlanMstatus(m);
    }
    //保养季度判断过期与任务重置
    @Scheduled(cron = "0 0 23 L 3,6,9,12 ?")
//    @Scheduled(cron = "0 */2 * * * *")
    public void updateMaintenanceQuarter() {
        MaintenanceAppVO m1=new MaintenanceAppVO();
        m1.setName("季度");
        List<MaintenanceAppVO> maintenanceList = appMapper.getMaintenanceListByName(m1);
        LocalDate today = LocalDate.now();
        for (MaintenanceAppVO item : maintenanceList) {
                    //修改为已过期
                    item.setMaintenanceStatus("2");
                    insertMhistory(item);
                    appMapper.updateMaintenanceProblemStatus(item);
        }
        MaintenancePlan m=new MaintenancePlan();
        m.setMaintenancePeriod("4");
        maintenanceMapper.updatePlanMstatus(m);
    }


    private static int convertChineseNumber(String chineseNum) {
        switch(chineseNum) {
            case "一": return 1;
            case "二": return 2;
            case "三": return 3;
            case "四": return 4;
            case "五": return 5;
            case "六": return 6;
            case "日": return 7;
            default: return -1;
        }
    }





//    @Scheduled(cron = "0 */10 * * * ?")
    public void updatePlanScheduleState() {
        PlanSchedule planSchedule = new PlanSchedule();
        List<PlanScheduleInfoBO> planScheduleInfoBOList = planScheduleMapper.selectPlanScheduleListwait(planSchedule);
        // 获取当前日期
        LocalDate today = LocalDate.now();
        // 获取当月的第几天（数字，如9）
        int dayOfMonth = today.getDayOfMonth();
        // 获取周几的数字（1=周一，7=周日）
        int dayOfWeekValue = today.getDayOfWeek().getValue();
        PlanSchedule planSchedule1 = new PlanSchedule();
        //未开始列表修改为运行状态
        for (PlanScheduleInfoBO planScheduleInfoBO : planScheduleInfoBOList) {
            //新增点检历史
            PlanScheduleHistory planScheduleHistory = new PlanScheduleHistory();
            planScheduleHistory.setPlanScheduleId(Long.valueOf(planScheduleInfoBO.getPlanScheduleId()));
            planScheduleHistory.setPlanAssociationId(Long.valueOf(planScheduleInfoBO.getPlanAssociationId()));
            planScheduleHistory.setScheduleId(Long.valueOf(planScheduleInfoBO.getScheduleId()));
            planScheduleHistory.setPeriod(planScheduleInfoBO.getPeriod());
            planScheduleHistory.setShiftDefaultId(Long.valueOf(planScheduleInfoBO.getShiftDefaultId()));
            planScheduleHistory.setCreateTime(LocalDate.now());

            if (planScheduleInfoBO.getTime().contains("日") && planScheduleInfoBO.getTime().length() < 6) {
                String input = planScheduleInfoBO.getTime();
                String day = input.replaceAll("[^0-9]", ""); // 提取数字部分
                if (Integer.parseInt(day) == dayOfMonth) {
                    //修改状态为运行中
                    planSchedule1.setPlanScheduleId(planScheduleInfoBO.getPlanScheduleId());
                    planSchedule1.setCheckStatus("运行中");
                    planScheduleMapper.updatePlanSchedule(planSchedule1);
                } else if (Integer.parseInt(day) < dayOfMonth) {
                    //修改状态为已过期
                    planSchedule1.setPlanScheduleId(planScheduleInfoBO.getPlanScheduleId());
                    planSchedule1.setCheckStatus("已过期");
                    planScheduleMapper.updatePlanSchedule(planSchedule1);

                    //修改历史状态
                    planScheduleHistory.setCheckStatus("已过期");
                    planScheduleHistoryMapper.insertPlanScheduleHistory(planScheduleHistory);
                }
            }
            if (planScheduleInfoBO.getTime().contains("周")) {
                if (dayOfWeekValue == getDayNumber(planScheduleInfoBO.getTime())) {
                    //修改状态为运行中
                    planSchedule1.setPlanScheduleId(planScheduleInfoBO.getPlanScheduleId());
                    planSchedule1.setCheckStatus("运行中");
                    planScheduleMapper.updatePlanSchedule(planSchedule1);
                } else if (dayOfWeekValue > getDayNumber(planScheduleInfoBO.getTime())) {
                    //修改状态为已过期
                    planSchedule1.setPlanScheduleId(planScheduleInfoBO.getPlanScheduleId());
                    planSchedule1.setCheckStatus("已过期");
                    planScheduleMapper.updatePlanSchedule(planSchedule1);

                    //修改历史状态
                    planScheduleHistory.setCheckStatus("已过期");
                    planScheduleHistoryMapper.insertPlanScheduleHistory(planScheduleHistory);
                }
            }
        }
    }




//    @Scheduled(cron = "0 0 0 * * ?")
//    @Scheduled(fixedDelay = 5000)
    public void PlanScheduleStatus() {
        PlanSchedule planSchedule = new PlanSchedule();
        List<PlanScheduleInfoBO> planScheduleInfoBOList = planScheduleMapper.selectPlanScheduleList(planSchedule);

        LocalDate today = LocalDate.now();
        //针对只有日的信息
        String format = today.format(DateTimeFormatter.ofPattern("d日"));
        //针对有月有日的信息
        String format1 = today.format(DateTimeFormatter.ofPattern("MM月dd日"));

        for (PlanScheduleInfoBO planScheduleInfoBO : planScheduleInfoBOList) {
            if (planScheduleInfoBO.getTime().contains("当日")) {
                planSchedule.setPlanScheduleId(planScheduleInfoBO.getPlanScheduleId());
                planSchedule.setCheckStatus("未开始");
                planScheduleMapper.updatePlanScheduleDeviceStatus(planSchedule);
            } else if (planScheduleInfoBO.getTime().contains("周")) {
                DayOfWeek dayOfWeek = today.getDayOfWeek();
                if (dayOfWeek.equals(DayOfWeek.MONDAY)) {
                    planSchedule.setPlanScheduleId(planScheduleInfoBO.getPlanScheduleId());
                    planSchedule.setCheckStatus("未开始");
                    planScheduleMapper.updatePlanScheduleDeviceStatus(planSchedule);
                }
            } else if (planScheduleInfoBO.getScheduleId() == 44 || planScheduleInfoBO.getScheduleId() == 45) {
                if (format.equals(planScheduleInfoBO.getTime())){
                    planSchedule.setPlanScheduleId(planScheduleInfoBO.getPlanScheduleId());
                    planSchedule.setCheckStatus("未开始");
                    planScheduleMapper.updatePlanScheduleDeviceStatus(planSchedule);
                }
            } else if (planScheduleInfoBO.getScheduleId() == 46 ||
                    planScheduleInfoBO.getScheduleId() == 47 ||
                    planScheduleInfoBO.getScheduleId() == 48){
                if (format1.equals(planScheduleInfoBO.getTime())){
                    planSchedule.setPlanScheduleId(planScheduleInfoBO.getPlanScheduleId());
                    planSchedule.setCheckStatus("未开始");
                    planScheduleMapper.updatePlanScheduleDeviceStatus(planSchedule);
                }
            }

        }
    }

    /**
     * 每日零点定时任务（开机自动执行一次PostConstruct）
     */
//    @Scheduled(cron = "0 */10 * * * ?")
    public void dailyMaintenanceTask() {
        List<MaintenanceAppVO> maintenanceList = appMapper.getMaintenanceList("");
        LocalDate today = LocalDate.now();

        for (MaintenanceAppVO item : maintenanceList) {
            switch (item.getMaintenancePeriod()) {
                case 1: // 每日
                    handleDailyOperation(item);
                    break;
                case 2: // 每周
                    handleWeeklyOperation(item, today);
                    break;
                case 3: // 每月
                    handleMonthlyOperation(item, today);
                    break;
                case 4: // 季度
                    handleQuarterlyOperation(item, today);
                    break;
                case 5: // 年度
                    handleYearlyOperation(item, today);
                    break;
            }
        }
    }


    private void handleDailyOperation(MaintenanceAppVO item) {
        // 每日维护操作逻辑
        item.setMaintenanceStatus("0");
        item.setRemark("");
        item.setProblemStatus("");
        updateMaintenanceStatus(item);
    }

    private void handleWeeklyOperation(MaintenanceAppVO item, LocalDate today) {
        if (today.getDayOfWeek().getValue() == 1) { // 每周一执行
            updateMaintenanceStatus(item);
        }
    }

    private void handleMonthlyOperation(MaintenanceAppVO item, LocalDate today) {
        if (today.getDayOfMonth() == 1) { // 每月1号执行
            updateMaintenanceStatus(item);
        }
    }

    private void handleQuarterlyOperation(MaintenanceAppVO item, LocalDate today) {
        int month = today.getMonthValue();
        if ((month == 1 || month == 4 || month == 7 || month == 10) && today.getDayOfMonth() == 1) {
            updateMaintenanceStatus(item);
        }
    }

    private void handleYearlyOperation(MaintenanceAppVO item, LocalDate today) {
        if (today.getMonthValue() == 1 && today.getDayOfMonth() == 1) { // 每年1月1日执行
            updateMaintenanceStatus(item);
        }
    }

    //数据库修改状态
    private void updateMaintenanceStatus(MaintenanceAppVO item) {
        appMapper.updateMaintenanceProblemStatus(item);
    }

    private static final Map<Integer, String> NUM_TO_DAY = new HashMap<>();

    static {
        NUM_TO_DAY.put(1, "周一");
        NUM_TO_DAY.put(2, "周二");
        NUM_TO_DAY.put(3, "周三");
        NUM_TO_DAY.put(4, "周四");
        NUM_TO_DAY.put(5, "周五");
        NUM_TO_DAY.put(6, "周六");
        NUM_TO_DAY.put(7, "周日");
    }

    private static final Map<String, Integer> DAY_TO_NUM = new HashMap<>();

    static {
        for (Map.Entry<Integer, String> entry : NUM_TO_DAY.entrySet()) {
            DAY_TO_NUM.put(entry.getValue(), entry.getKey());
        }
    }

    public static String getDayName(int num) {
        int adjustedNum = ((num - 1) % 7 + 7) % 7 + 1; // 循环逻辑处理负数和大数
        return NUM_TO_DAY.getOrDefault(adjustedNum, "无效数字");
    }

    public static int getDayNumber(String dayName) {
        return DAY_TO_NUM.getOrDefault(dayName, -1); // 返回-1表示无效输入
    }
}
